package com.view.sellmanage;

import com.bean.*;
import com.lovo.netCRM.component.*;
import com.service.CustomerService;
import com.service.SellService;
import com.service.impl.CustomerServiceImpl;
import com.service.impl.SellServiceImpl;
import com.view.MainFrame;
import com.view.purchasemanage.GoodsAddDialog;
import com.view.purchasemanage.GoodsUpdateDialog;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class SellOutManagePanel extends JPanel {
    private LovoComboBox<Customer> cilentComb;
    private LovoLabel principal;
    private LovoLabel inGoods;
    private LovoLabel money;
    private LovoTxt place;

    private MainFrame frame;

    private LovoTable purInTab;

    private LovoButton addGoodsBtn;
    private LovoButton delGoodsBtn;
    private LovoButton outGoodsBtn;
    private LovoButton updateBtn;
    private User user;


    private CustomerService customerService = new CustomerServiceImpl();
    private SellService sellService = new SellServiceImpl();

    public SellOutManagePanel(MainFrame frame){
        this.frame = frame;
        this.user = frame.getUser();
        this.setBackground(Color.WHITE);
        this.setLayout(null);
        this.place = new LovoTxt("地址",350,100,this);
        place.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                
            }

            @Override
            public void removeUpdate(DocumentEvent e) {

            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });
        cmbInit();
        this.initTable();
        this.init();
    }

    private void initTable(){
        this.purInTab = new LovoTable(this, new String[]{"商品名称","厂家","建议采购价","建议销售价","采购数量"},
                new String[]{"goods.name","goods.manufactor","goods.buyPrice","goods.sellPrice","number"},//数组中应该是对应属性的属性名
                "id");//填入唯一标示属性
        List<SellInfo> sellInfos = new ArrayList<>();
        Customer customer = cilentComb.getItem();
        String address = place.getText();
        sellInfos = sellService.getAllGoods(frame.getUser(),customer,address);
        purInTab.updateLovoTable(sellInfos);
        this.purInTab.setSizeAndLocation(20, 150, 750, 200);
        //bindTable();
    }

    public void cmbFlush(){
         List<Customer> customerList = new ArrayList<>();
         customerList = customerService.getAllCustomer();
         cilentComb.setList(customerList);
    }

    public void cmbInit(){
        List<Customer> customerList = new ArrayList<>();
        customerList = customerService.getAllCustomer();
        this.cilentComb = new LovoComboBox<>("客户", customerList,50,50,this);
        cilentComb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<SellInfo> sellInfos = new ArrayList<>();
                Customer customer =cilentComb .getItem();
                String address = place.getText();
                sellInfos = sellService.getAllGoods(frame.getUser(),customer,address);
                purInTab.updateLovoTable(sellInfos);

                String newmoney =String.valueOf(sellService.getMoney(user,customer,address)) ;
                money.setText("<html>"+newmoney+"</html>");
            }
        });
    }

    private void init(){
        String userName = user.getRealName();
        this.principal = new LovoLabel("<html>负责人  "+userName+"</html>",350,50,this);
        this.money = new LovoLabel("订单总额",550,50,this);
        this.inGoods = new LovoLabel("出库物品",50,100,this);

        this.addGoodsBtn = new LovoButton("添加商品", 100, 400, this);
        this.delGoodsBtn = new LovoButton("移除商品", 250, 400, this);
        this.updateBtn = new LovoButton("修改数量", 400, 400, this);
        this.outGoodsBtn = new LovoButton("出库",550,400,this);

        addGoodsBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String address = place.getText();
                Customer customer = cilentComb.getItem();
                SellGoodsAddDialog sellGoodsAddDialog = new SellGoodsAddDialog(frame,address,customer);
            }
        });

        delGoodsBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = purInTab.getKey();
                if(key == -1 ){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else {
                    int m = JOptionPane.showConfirmDialog(null,"你确定要删除吗？");
                    if(m == 0 ){
                        if(sellService.deleteGoods(key)){
                            JOptionPane.showMessageDialog(null,"删除成功");
                            frame.flush();
                        }else {
                            JOptionPane.showMessageDialog(null,"删除失败");
                        }
                    }
                }            }
        });

        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = purInTab.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else{
                    SellGoodsUpdateDialog sellGoodsUpdateDialog = new SellGoodsUpdateDialog(frame,key);
                }
            }
        });

        outGoodsBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Customer customer = cilentComb.getItem();
                String address = place.getText();
                if(sellService.outWarehouse(user,customer,address)){
                    JOptionPane.showMessageDialog(null,"出库成功");
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"出库失败");
                }
            }
        });
    }

    public Sell getSell(){
        User user = frame.getUser();
        Customer customer = cilentComb.getItem();
        String address = place.getText();
        Sell sell = sellService.sellInit(user,customer,address);
        return sell;
    }

    public void tableFlush(){
        List<SellInfo> sellInfos = new ArrayList<>();
        Customer customer = cilentComb.getItem();
        String address = place.getText();
        sellInfos = sellService.getAllGoods(frame.getUser(),customer,address);
        purInTab.updateLovoTable(sellInfos);

        String newmoney =String.valueOf(sellService.getMoney(user,customer,address)) ;
        money.setText("<html>"+newmoney+"</html>");
    }

}
